package com.sec.android.app.myfiles.widget.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.RelativeLayout;

import com.sec.android.app.myfiles.info.AppConstants;

public class CheckedRelativeLayout extends RelativeLayout implements Checkable {
    private Checkable mCheckable;

    private int mCheckableId;

    private static final String ATT_CHECKABLE = "checkableId";

    public CheckedRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCheckableId = attrs.getAttributeResourceValue(AppConstants.MY_FILES_NAME_SPACE, ATT_CHECKABLE, 0);
    }

    @Override
    public boolean isChecked() {
        mCheckable = (Checkable) findViewById(mCheckableId);
        if (mCheckable != null) {
            return mCheckable.isChecked();
        }
        return false;
    }

    /**
     * <p>
     * Changes the checked state of this text view.
     * </p>
     *
     * @param checked true to check the text, false to uncheck it
     */
    @Override
    public void setChecked(boolean checked) {
        mCheckable = (Checkable) findViewById(mCheckableId);
        if (mCheckable != null) {
            mCheckable.setChecked(checked);
            setActivated(isChecked());
        }
    }

    @Override
    public void toggle() {
        mCheckable = (Checkable) findViewById(mCheckableId);
        if (mCheckable != null) {
            mCheckable.toggle();
        }
    }
}
